<?php


namespace app\api\model;


use fast\Http;

class Clue extends Base
{
    /**
     * @param int $log_id 进线日志id
     * @param string $error 进线失败原因
     * @return int|string
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public static function setClueApiLogError($log_id, $error = "")
    {
        $result = \db('clue_api_log')->where('id',$log_id)->update(['error' => $error]);
        return $result;
    }

    /**
     * 获取手机号码归属地
     */
    public function getMobileCity($mobile)
    {
        if (empty($mobile) || strlen($mobile) != 11) {
            return false;
        }
        $url = "https://ec8a.api.huachen.cn/mobile";
        $appcode = "bef01d971b9c4c719b7cf10d51a5737c";
        $params = ['mobile' => $mobile];

        $result = Http::sendRequest($url, $params,'GET', [CURLOPT_HTTPHEADER =>['Authorization:APPCODE '. $appcode]]);
        if (empty($result) || $result['ret'] === false || empty($result['msg'])) {
            return false;
        }
        $res = json_decode($result['msg'], true);
        // 消息状态码 200成功，否则失败
        if ($res['ret'] != 200) {
            writelog("失败日志==".json_encode($params, JSON_UNESCAPED_UNICODE). "====". $result['msg']. "====". $url, 'mobileCity');
            return false;
        }
        $city = \db('city')->where(['type' => 2, 'name' => $res['data']['city']])->find();
        if (empty($city)) {
            writelog("城市失败日志==" .$result['msg']. "====", 'mobileCity');
            return false;
        }
        return $city['pid']. "-" . $city['id'];
    }
}